package reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

// 获得类的信息
public class Test4 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class<?> c1 = Class.forName("reflection.User");

        // 获得类的名字
        System.out.println(c1.getName());
        System.out.println(c1.getSimpleName());
        /*
            reflection.User
            User
         */

        // 获得类的属性
        System.out.println("============");
        Field[] fields = c1.getFields();
        fields = c1.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        /*
            private java.lang.String reflection.User.name
            private int reflection.User.age
         */


        // 获得指定属性的值
        System.out.println("============");
        Field name = c1.getDeclaredField("name");
        System.out.println(name);
        /*
            private java.lang.String reflection.User.name
         */


        // 获得类的方法
        System.out.println("=============");
        Method[] methods = c1.getMethods();
        for (Method method : methods) {
            System.out.println("正常的："+method);
        }
        methods = c1.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println("getDeclareMethods" + method);
        }
        /*
            正常的：public java.lang.String reflection.User.toString()
            正常的：public java.lang.String reflection.User.getName()
            正常的：public void reflection.User.setName(java.lang.String)
            正常的：public int reflection.User.getAge()
            正常的：public void reflection.User.setAge(int)
            正常的：public final void java.lang.Object.wait() throws java.lang.InterruptedException
            正常的：public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
            正常的：public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
            正常的：public boolean java.lang.Object.equals(java.lang.Object)
            正常的：public native int java.lang.Object.hashCode()
            正常的：public final native java.lang.Class java.lang.Object.getClass()
            正常的：public final native void java.lang.Object.notify()
            正常的：public final native void java.lang.Object.notifyAll()
            getDeclareMethodspublic java.lang.String reflection.User.toString()
            getDeclareMethodspublic java.lang.String reflection.User.getName()
            getDeclareMethodspublic void reflection.User.setName(java.lang.String)
            getDeclareMethodspublic int reflection.User.getAge()
            getDeclareMethodspublic void reflection.User.setAge(int)
         */

        // 获得指定方法
        System.out.println("=============");
        Method getName = c1.getMethod("getName",null);
        Method setName = c1.getMethod("setName",String.class);
        System.out.println(getName);
        System.out.println(setName);
        /*
            public java.lang.String reflection.User.getName()
            public void reflection.User.setName(java.lang.String)
         */

        // 获得指定的构造器
        System.out.println("=============");
        Constructor<?>[] constructors = c1.getConstructors();  // 获得public方法
        for (Constructor<?> constructor : constructors) {
            System.out.println(constructor);
        }
        constructors = c1.getDeclaredConstructors();  // 获得全部方法
        for (Constructor<?> constructor : constructors) {
            System.out.println("#"+constructor);
        }
        /*
            public reflection.User()
            public reflection.User(java.lang.String,int)
            #public reflection.User()
            #public reflection.User(java.lang.String,int)
         */


        // 获得指定的构造器
        System.out.println("=============");
        Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class);
        System.out.println(""+declaredConstructor);
        /*
            public reflection.User(java.lang.String,int)
         */


    }
}
